package com.qf04;

import java.util.Arrays;

/**
 * 线程安全
 */
public class ThreadSafe {
    private static int index=0;
    public static void main(String[] args) throws InterruptedException {
        //创建数组
        String[] s=new String[5];
        Runnable runnableA = new Runnable() {

            @Override
            public void run() {
                synchronized (s){
                    s[index]="hello";
                    index++;
                }
            }
        };

        Runnable runnableB = new Runnable() {

            @Override
            public void run() {
                synchronized (s){
                    s[index]="world";
                    index++;
                }
            }
        };

        Thread a=new Thread(runnableA);
        Thread b=new Thread(runnableB);
        a.start();
        b.start();
        a.join();
        b.join();

        System.out.println(Arrays.toString(s));
    }
}
